/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mailctrl;

/**
 *http://developer.51cto.com/art/200903/117512.htm
 * @author flintx
 */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.jdom.Element;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



public class MailParser implements XmlDocument{

    String content;
    Date sendDate;
    public MailParser()
    {
    }
    public MailParser(String content)
    {
        this.content = content;
    }
    public void setSendDate(Date date)
    {
        this.sendDate = date;
    }

    public void createXml(String filename)
    {
        System.out.println("<<"+filename+">>");
    }

    public void parserXml(InputStream is)
    {
        SAXParserFactory saxfac = SAXParserFactory.newInstance();
        try{
            SAXParser saxparser = saxfac.newSAXParser();
            saxparser.parse(is, new MySAXHandler(this.sendDate));
        }catch(ParserConfigurationException e){
            e.printStackTrace();
        }catch(SAXException e){
            e.printStackTrace();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public void parserXml(String filename)
    {
        SAXParserFactory saxfac = SAXParserFactory.newInstance();
        try{
            SAXParser saxparser = saxfac.newSAXParser();
            InputStream is = new FileInputStream(filename);
            saxparser.parse(is, new MySAXHandler(this.sendDate));
        }catch(ParserConfigurationException e){
            e.printStackTrace();
        }catch(SAXException e){
            e.printStackTrace();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

class MySAXHandler extends DefaultHandler{

    boolean hasAttribute = false;
    Attributes attributes = null;
    MailOperPool pool;
    String value;
    public MySAXHandler(Date date)
    {
         pool = new MailOperPool();
         pool.setSendDate(date);
         pool.setLogFile("logfile.xml");
         pool.openLogFile();
    }
    @Override
    public void startDocument()throws SAXException{
        System.out.println("parser processing ... ...");
    }
    @Override
    public void endDocument()throws SAXException{
        System.out.println("end for parse document");
    }
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
        if(qName.equals("Name"))
        {
            //System.out.println(localName);
            return;
        }
        if(qName.equals("Path"))
        {
            //System.out.println(localName);
            return;
        }
        if(attributes.getLength() > 0)
        {
            this.attributes = attributes;
            this.hasAttribute = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException
    {
        /*if(hasAttribute && (attributes != null))
        {
            for(int i=0; i<attributes.getLength(); i++)
            {
                System.out.print(attributes.getQName(0) + attributes.getValue(0));

            }
        }*/
        if(qName.equals("Name"))
        {
            pool.setAppName(value);
             return;
        }
        if(qName.equals("Path"))
        {
           pool.setAppPath(value);
            return;
        }
        if(qName.equals("Operation"))
        {
            Element et = pool.packageOperation();
            pool.pushElement(et);
            pool.closeLogFile();
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException
    {
        value = new String(ch, start, length);
    }
}


